//
//  RBGFiltreController.m
//  BateauGenerique
//
//  Created by Kevin De Jesus Ferreira on 31/05/2014.
//  Copyright (c) 2014 Kevin De Jesus Ferreira. All rights reserved.
//

#import "RBGFiltreController.h"
#import "RBGNavigationBar.h"
#import "RBGColorManager.h"
#import "RBGAppDelegate.h"
#import "UIImageView+WebCache.h"
#import "Constantes.h"
#import "RBGSelectorValue.h"
#import "Categorie.h"
#import "Etat.h"
#import "RBGServiceChargement.h"
#import "Region.h"
#import "RBGMarqueModeleController.h"
#import "RBGRangeViewController.h"
#import "Marque.h"


@interface RBGFiltreController ()<RBGSelectorValueDelegate,RBGServiceChargementDelegate,RBGMarqueModeleDelegate,RBGRangeDelegate>{
    RBGAppDelegate *appDelegate;
    
    NSMutableArray *facultatif;
    NSMutableArray *typesString;
    NSMutableArray *etatsString;
    NSMutableArray *regionString;
    NSMutableArray *marquesString;
    
    RBGSelectorValue *selectorValue;
    ;
    NSArray * typesAn;
    
    
}


@end

@implementation RBGFiltreController


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self charger];
    [self remplir];
    [self chargerCouleur];
}

-(void) viewWillAppear:(BOOL)animated
{
    [self positionnerVue];
    self.navigationController.navigationBarHidden = NO;
    
}

-(void)charger
{
   
    appDelegate.decalage = 15;
    appDelegate = (RBGAppDelegate *)[[UIApplication sharedApplication] delegate];
    
    [self.fond setImageWithURL:[NSURL URLWithString:appDelegate.donnees.parametres.imageFond]];
    
    typesString = [[NSMutableArray alloc]init];
    
    regionString = [[NSMutableArray alloc]init];
    marquesString = [[NSMutableArray alloc]init];
    NSLog(@"TYPE ANNONCE %01d",self.typeAnnonce);
    typesAn = [appDelegate.donnees.typeCategories objectAtIndex:self.typeAnnonce - 1];
    
    for(Categorie *t in typesAn){
        [typesString addObject:t.libelle];
    }
    
    for(Region * r in appDelegate.donnees.regions){
        [regionString addObject:r.nom];
        
    }
    
    
        for(Marque * m in [appDelegate.donnees.marques objectForKey:[NSString stringWithFormat:@"%01d",self.typeAnnonce]]){
            [marquesString addObject:m.libelle];
        }


    
    selectorValue = [[RBGSelectorValue alloc] initWithNibName:@"RBGSelectorValue" bundle:nil];
    selectorValue.delegate = self;
    
    UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Effacer" style:UIBarButtonItemStylePlain target:self action:@selector(effacer)];
    self.navigationItem.rightBarButtonItem = anotherButton;
    
    
    facultatif = [[NSMutableArray alloc]init];
    
    self.title= @"Recherche";
    
    NSLog(@"Marque id %@",appDelegate.marque_id);

    
    [self rafraichirVue];
    
}

-(void) remplir
{
    
    
    if(self.valueCategorie != nil)
    [facultatif addObject:self.valueCategorie];
    
    if(self.valueEtat != nil)
    [facultatif addObject:self.valueEtat];
  
    if(self.valueRegion != nil)
    [facultatif addObject:self.valueRegion];
    
    if(self.valueLongueur != nil)
    [facultatif addObject:self.valueLongueur];
    
    if(self.valueChantierModele != nil)
    [facultatif addObject:self.valueChantierModele];
 
    if(self.valuePrix != nil)
    [facultatif addObject:self.valuePrix];
  
    
    if(self.valueMarque != nil)
    [facultatif addObject:self.valueMarque];
  
    if(self.valuePuissance != nil)
    [facultatif addObject:self.valuePuissance];
    
    
    if(self.valueCategorieMoteur != nil)
    [facultatif addObject:self.valueCategorieMoteur];
    
    if(self.valueMarqueMoteur != nil)
    [facultatif addObject:self.valueMarqueMoteur];
   
    
    if(self.valuePuissanceMoteur != nil)
        [facultatif addObject:self.valuePuissanceMoteur];
    
    if(self.valueEtatMoteur != nil)
    [facultatif addObject:self.valueEtatMoteur];
    
    if(self.valueRegionMoteur != nil)
    [facultatif addObject:self.valueRegionMoteur];
    
    if(self.valuePrixMoteur != nil)
    [facultatif addObject:self.valuePrixMoteur];
    
    if(self.valueCategorieDivers != nil)
    [facultatif addObject:self.valueCategorieDivers];
    
    if(self.valueRegionDivers != nil)
    [facultatif addObject:self.valueRegionDivers];
    
    if(self.valueDiversPrix != nil)
    [facultatif addObject:self.valueDiversPrix];
}

-(void) effacer
{
    [self effacerValues:facultatif withValue:@"facultatif"];
}

-(void) chargerCouleur
{
    [RBGColorManager changeTextColorSubViews:self.attributBateau withColor:appDelegate.donnees.parametres.fontColorDeux];
    
    [RBGColorManager changeImageColorSubViews:self.attributBateau withColor:appDelegate.donnees.parametres.fontColorDeux];
    
    [RBGColorManager changeTextColorSubViews:self.attributMoteur withColor:appDelegate.donnees.parametres.fontColorDeux];
    
    [RBGColorManager changeImageColorSubViews:self.attributMoteur withColor:appDelegate.donnees.parametres.fontColorDeux];
    
    [RBGColorManager changeTextColorSubViews:self.attributDivers withColor:appDelegate.donnees.parametres.fontColorDeux];
    
    [RBGColorManager changeImageColorSubViews:self.attributDivers withColor:appDelegate.donnees.parametres.fontColorDeux];
    
    [self.cancel setBackgroundColor:appDelegate.donnees.parametres.backgroundColorDeux];
    [self.cancel setTitleColor:appDelegate.donnees.parametres.fontColorDeux forState:UIControlStateNormal];
    
    [self.search setBackgroundColor:appDelegate.donnees.parametres.backgroundColorDeux];
    
    [self.search setTitleColor:appDelegate.donnees.parametres.fontColorDeux forState:UIControlStateNormal];
    
    
}

-(void)positionnerVue{
    [self.cancel setFrame:CGRectMake(-2000, 0, 0, 0)];
    
    
    if(self.typeAnnonce == TYPE_ANNONCE_BATEAU_A_MOTEUR || self.typeAnnonce == TYPE_ANNONCE_SEMI_RIGIDE
       || self.typeAnnonce == TYPE_ANNONCE_VOILIER){
        
        [self.attributMoteur setFrame:CGRectMake(-2000, 0, 0, 0)];
        [self.attributDivers setFrame:CGRectMake(-2000, 0, 0, 0)];
        
    }
    
    if(self.typeAnnonce == TYPE_ANNONCE_MOTEUR){
        
        
        [self.attributBateau setFrame:CGRectMake(-2000, 0, 0, 0)];
        [self.attributDivers setFrame:CGRectMake(-2000, 0, 0, 0)];
    }
    
    if(self.typeAnnonce == TYPE_ANNONCE_ACCESSOIRE || self.typeAnnonce == TYPE_ANNONCE_PLACE_DE_PORT
       || self.typeAnnonce == TYPE_ANNONCE_LOCATION || self.typeAnnonce == TYPE_ANNONCE_REMORQUES){
        
        [self.attributMoteur setFrame:CGRectMake(-2000, 0, 0, 0)];
        [self.attributBateau setFrame:CGRectMake(-2000, 0, 0, 0)];
        
    }
    
    [self.search setFrame:CGRectMake(0, self.search.frame.origin.y+appDelegate.decalage, self.search.frame.size.width, self.search.frame.size.height)];
    
    appDelegate.decalage = 0;
    
}

- (IBAction)actionCancel:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)actionSearch:(id)sender {
    
    [self.delegate receiveDonneesFiltre:[self recupererDonnees]];
    
    [self.navigationController popViewControllerAnimated:YES];
}


-(NSMutableDictionary *) recupererDonnees{
    
    NSMutableDictionary * arguments = [[NSMutableDictionary alloc]init];
    
    
    if(appDelegate.categorie_id != nil && ![appDelegate.categorie_id isEqualToString:@"-1"]){
        
        [arguments addEntriesFromDictionary:@{
                                              ANNONCES_CATEGORIE_ID:[NSString stringWithFormat:@"%@",appDelegate.categorie_id]
                                              }];
    }
    
    if(appDelegate.region_id != nil && ![appDelegate.region_id isEqualToString:@"-1"]){
        
        [arguments addEntriesFromDictionary:@{
                                              ANNONCES_REGION_ID:[NSString stringWithFormat:@"%@",appDelegate.region_id]
                                              }];
    }
    
    if(appDelegate.longueur_min_range != nil && appDelegate.longueur_max_range != nil){
        
        if(![appDelegate.longueur_max_range isEqualToString:@"+"]){
            [arguments addEntriesFromDictionary:@{
                                                  ANNONCES_MAX_TAILLE:[NSString stringWithFormat:@"%@",appDelegate.longueur_max_range]
                                                  }];
        }
        
        if(![appDelegate.longueur_min_range isEqualToString:@"0"]){
            [arguments addEntriesFromDictionary:@{
                                                  ANNONCES_MIN_TAILLE:[NSString stringWithFormat:@"%@",appDelegate.longueur_min_range]
                                                  }];
        }
        
    }
    
    if(appDelegate.prix_min_range != nil && appDelegate.prix_max_range != nil){
        
        if(![appDelegate.prix_max_range isEqualToString:@"+"]){
            [arguments addEntriesFromDictionary:@{
                                                  ANNONCES_MAX_PRIX:[NSString stringWithFormat:@"%@",appDelegate.prix_max_range]
                                                  }];
        }
        
        if(![appDelegate.prix_min_range isEqualToString:@"0"]){
            [arguments addEntriesFromDictionary:@{
                                                  ANNONCES_MIN_PRIX:[NSString stringWithFormat:@"%@",appDelegate.prix_min_range]
                                                  }];
        }
        
    }
    
    if(appDelegate.puissance_min_range != nil && appDelegate.puissance_max_range != nil){
        
        if(![appDelegate.puissance_max_range isEqualToString:@"+"]){
            [arguments addEntriesFromDictionary:@{
                                                  ANNONCES_MAX_PUISS:[NSString stringWithFormat:@"%@",appDelegate.puissance_max_range]
                                                  }];
        }
        
        if(![appDelegate.puissance_min_range isEqualToString:@"0"]){
            [arguments addEntriesFromDictionary:@{
                                                  ANNONCES_MIN_PUISS:[NSString stringWithFormat:@"%@",appDelegate.puissance_min_range]
                                                  }];
        }
        
    }
    
    if(appDelegate.etat_id != nil && ![appDelegate.etat_id isEqualToString:@"-1"]){
        
        [arguments addEntriesFromDictionary:@{
                                              ANNONCES_ETAT:[NSString stringWithFormat:@"%@",appDelegate.etat_id]
                                              }];
    }
    
    if(appDelegate.modele_id != nil && ![appDelegate.modele_id isEqualToString:@"-1"]){
        
        [arguments addEntriesFromDictionary:@{
                                              ANNONCES_MODELE_ID:[NSString stringWithFormat:@"%@",appDelegate.modele_id]
                                              }];
    }
    
    if(appDelegate.marque_id != nil && ![appDelegate.marque_id isEqualToString:@"-1"]){
        
        [arguments addEntriesFromDictionary:@{
                                              ANNONCES_MARQUE_ID:[NSString stringWithFormat:@"%@",appDelegate.marque_id]
                                              }];
    }
    
    return arguments;
}

/*
 protected List<NameValuePair> recupererDonnees(){
 
 
 if(typeAnnonces.equals(Constantes.BATEAUX)){
 
 if(recherche_chantier_id != null  && !this.recherche_chantier_id.equals("-1"))
 Net.add(donnees, Constantes.ANNONCES_MARQUE_ID,recherche_chantier_id);
 }
 else if(typeAnnonces.equals(Constantes.MOTEURS) && recherche_marque_id != null && !this.recherche_marque_id.equals("-1"))
 Net.add(donnees, Constantes.ANNONCES_MARQUE_ID,recherche_marque_id);
 
 return donnees;
 
 }
 
 */
- (IBAction)changeClearColor:(UIButton *)sender
{
    [sender setBackgroundColor:[UIColor clearColor]];
    
}
- (IBAction)changeColor:(id)sender{
    
    [sender setBackgroundColor:appDelegate.donnees.parametres.backgroundColorDeux];
}

- (IBAction)demanderValue:(UIButton *)sender {
    
    switch (sender.tag) {
        case FILTRE_CATEGORIE:{
            NSLog(@"FILTRE_CATEGORIE");
            [self demanderType];
        }
            break;
        case FILTRE_CHANTIER_MODELE:{
            NSLog(@"FILTRE_CHANTIER_MODELE");
            [self demancherChantier];
            
        }
            break;
        case FILTRE_LONGUEUR:{
            NSLog(@"FILTRE_LONGUEUR");
            [self demanderLongueur];
        }
            break;
        case FILTRE_ETAT:{
            NSLog(@"FILTRE_ETAT");
            [self demanderEtat];
        }
            break;
        case FILTRE_REGION:{
            NSLog(@"FILTRE_REGION");
            [self demanderRegion];
            
        }
            break;
        case FILTRE_PRIX:{
            
            [self demanderPrix];
            
            NSLog(@"FILTRE_PRIX");
            
        }
            break;
        case FILTRE_MARQUE:{
            
            NSLog(@"FILTRE_MARQUE");
            [self demanderMarque];
        }
            break;
        case FILTRE_PUISSANCE:{
            NSLog(@"FILTRE_PUISSANCE");
            [self demanderPuissance];
        }
            break;
    }
}

#pragma mark - Transparent Modal View
-(void) presentTransparentModalViewController: (UIViewController *) aViewController
                                     animated: (BOOL) isAnimated
                                    withAlpha: (CGFloat) anAlpha{
    
    UIView *view = aViewController.view;
    
    view.opaque = NO;
    view.alpha = anAlpha;
    [view.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        UIView *each = obj;
        each.opaque = NO;
        each.alpha = anAlpha;
    }];
    
    if (isAnimated) {
        //Animated
        CGRect mainrect = [[UIScreen mainScreen] bounds];
        CGRect newRect = CGRectMake(0, mainrect.size.height, mainrect.size.width, mainrect.size.height);
        
        
        [self.view addSubview:view];
        view.frame = newRect;
        
        [UIView animateWithDuration:0.8
                         animations:^{
                             view.frame = mainrect;
                         } completion:^(BOOL finished) {
                             //nop
                         }];
        
    }else{
        view.frame = [[UIScreen mainScreen] bounds];
        [self.view addSubview:view];
    }
    
}

#define TYPE 0
#define ETAT 3
#define REGION 4
#define MARQUES 5

-(void) demanderType
{
    
    selectorValue.values = [typesString mutableCopy];
    selectorValue.facultatif = YES;
    selectorValue.type = TYPE;
    [self.navigationController pushViewController:selectorValue animated:YES];
    
    
    // [self presentViewController:selectorValue animated:NO completion:nil];
    
}

/*
 
 if(typeAnnonces.equals(Constantes.MOTEURS)){
 List<Marque> marques = Donnees.getMarques(""+typeAnnonces,true);
 Map<String,String> donneesValeurs = new HashMap<String, String>();
 for(Marque m : marques){
 donneesValeurs.put(m.getLibelle(), m.getId());
 }
 ajouterFragment(new DonneeValeurSelector(this, MARQUE, donneesValeurs));
 }else{
 ajouterFragment(new MarqueSelector(this,MARQUE,""+typeAnnonces,true));
 }
 
 }
 
 */

-(void) demanderMarque
{
    
    selectorValue.values = [marquesString mutableCopy];
    selectorValue.facultatif = YES;
    selectorValue.type = MARQUES;
    [self.navigationController pushViewController:selectorValue animated:YES];
}

-(void)demanderEtat
{
    if(appDelegate.donnees.etats == nil || [appDelegate.donnees.etats count] == 0){
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
        
        // On va lancer le delegate
        RBGServiceChargement * chargement = [[RBGServiceChargement alloc]init];
        chargement.delegate = self;
        [chargement chargerEtats];
        
    }else{
        
        if(etatsString == nil){
            etatsString = [[NSMutableArray alloc]init];
            for(Etat * e in appDelegate.donnees.etats){
                NSLog(@"%@",e.nomEtat);
                [etatsString addObject:e.nomEtat];
            }
        }
        
        selectorValue.values = [etatsString mutableCopy];
        selectorValue.facultatif = YES;
        selectorValue.type = ETAT;
        [self.navigationController pushViewController:selectorValue animated:YES];
        
    }
    
}

-(void) demanderRegion
{
    
    if(appDelegate.donnees.regions == nil || [appDelegate.donnees.regions count] == 0){
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
        
        // On va lancer le delegate
        RBGServiceChargement * chargement = [[RBGServiceChargement alloc]init];
        chargement.delegate = self;
        [chargement chargerRegions];
        
    }else{
        
        if(regionString == nil){
            regionString = [[NSMutableArray alloc]init];
            for(Region * r in appDelegate.donnees.regions){
                [regionString addObject:r.nom];
            }
        }
        
        selectorValue.values = [regionString mutableCopy];
        selectorValue.facultatif = YES;
        selectorValue.type = REGION;
        [self.navigationController pushViewController:selectorValue animated:YES];
        
    }
    
}

-(void) demanderPrix
{
    appDelegate.type_range = FILTRE_PRIX;
    
    if(self.typeAnnonce == TYPE_ANNONCE_BATEAU_A_MOTEUR || self.typeAnnonce == TYPE_ANNONCE_VOILIER || self.typeAnnonce == TYPE_ANNONCE_SEMI_RIGIDE){
        [RBGAppDelegate setMinValue:0];
        [RBGAppDelegate setMaxValue:200000];
        
    }else{
        
        [RBGAppDelegate setMinValue:0];
        [RBGAppDelegate setMaxValue:20000];
        
    }
    
    RBGRangeViewController * controller = [[RBGRangeViewController alloc]init];
    
    
    // Do any additional setup after loading the view.
    self.transitionController = [[TransitionDelegate alloc] init];
    controller.view.backgroundColor = [UIColor clearColor];
    [controller setTransitioningDelegate:self.transitionController];
    controller.modalPresentationStyle= UIModalPresentationCustom;
    controller.alphaFond = 0.5;
    controller.cancelable = YES;
    controller.titrePopUp = @"Prix Range";
    controller.delegate = self;
    
    [self presentViewController:controller animated:YES completion:nil];
}

-(void) demanderPuissance
{
    appDelegate.type_range = FILTRE_PUISSANCE;
    
    [RBGAppDelegate setMinValue:0];
    [RBGAppDelegate setMaxValue:300];
    
    
    
    RBGRangeViewController * controller = [[RBGRangeViewController alloc]init];
    
    
    // Do any additional setup after loading the view.
    self.transitionController = [[TransitionDelegate alloc] init];
    controller.view.backgroundColor = [UIColor clearColor];
    [controller setTransitioningDelegate:self.transitionController];
    controller.modalPresentationStyle= UIModalPresentationCustom;
    controller.alphaFond = 0.5;
    controller.cancelable = YES;
    controller.titrePopUp = @"Puissance Range";
    controller.delegate = self;
    
    [self presentViewController:controller animated:YES completion:nil];
}

-(void) demanderLongueur
{
    appDelegate.type_range = FILTRE_LONGUEUR;
    [RBGAppDelegate setMinValue:0];
    [RBGAppDelegate setMaxValue:16];
    
    RBGRangeViewController * controller = [[RBGRangeViewController alloc]init];
    
    
    // Do any additional setup after loading the view.
    self.transitionController = [[TransitionDelegate alloc] init];
    controller.view.backgroundColor = [UIColor clearColor];
    [controller setTransitioningDelegate:self.transitionController];
    controller.modalPresentationStyle= UIModalPresentationCustom;
    controller.alphaFond = 0.5;
    controller.cancelable = YES;
    controller.titrePopUp = @"Longueur Range";
    controller.delegate = self;
    
    [self presentViewController:controller animated:YES completion:nil];
}

-(void) demancherChantier
{
    
    RBGMarqueModeleController * controller = [[RBGMarqueModeleController alloc]init];
    controller.typeAnnnonce = self.typeAnnonce - 1;
    controller.delegate = self;
    controller.wa = YES;
    controller.filtreController = self;
    [self.navigationController pushViewController:controller animated:YES];
    
}

-(void) rafraichirVue{
    
    if(appDelegate.categorie_id  != nil)
        [self receiveValueSelected:appDelegate.selectedValueCategorie andTypeValeur:TYPE];
    
    if(appDelegate.etat_id  != nil)
        [self receiveValueSelected:appDelegate.selectedValueEtat andTypeValeur:ETAT];
    
    if(appDelegate.region_id  != nil)
        [self receiveValueSelected:appDelegate.selectedValueRegion andTypeValeur:REGION];
    
    if(appDelegate.marque_id  != nil && appDelegate.modele_id == nil)
        [self receiveValueSelected:appDelegate.selectedValueMarques andTypeValeur:MARQUES];
    
    if(appDelegate.prix_max_range != nil || appDelegate.prix_min_range != nil){
        appDelegate.type_range = FILTRE_PRIX;
        [self receiveDonneesRBGRange:appDelegate.prix_min_range andNomModele:appDelegate.prix_max_range];
        
    }
    
    if(appDelegate.longueur_min_range != nil || appDelegate.longueur_max_range != nil){
        appDelegate.type_range = FILTRE_LONGUEUR;
        [self receiveDonneesRBGRange:appDelegate.longueur_min_range andNomModele:appDelegate.longueur_max_range];
        
    }
    
    if(appDelegate.puissance_min_range != nil || appDelegate.puissance_max_range != nil){
        appDelegate.type_range = FILTRE_PUISSANCE;
        [self receiveDonneesRBGRange:appDelegate.puissance_min_range andNomModele:appDelegate.puissance_max_range];
        
    }
    
    NSLog(@"Marque id %@",appDelegate.marque_id);

    
    if(appDelegate.marque_id != nil && appDelegate.modele_id != nil){
        [self receiveDonneesRBGMarqueModele:appDelegate.marque_id andNomMarque:appDelegate.libelleMarque andIdModele:appDelegate.modele_id andNomModele:appDelegate.libelleModele];
    }
    
}

- (void) receiveDonneesRBGMarqueModele:(NSString *)idMarque
                          andNomMarque:(NSString *)libelleMarque
                           andIdModele:(NSString *)idModele
                          andNomModele:(NSString *)libelleModele{
    [self.valueChantierModele setText:[NSString stringWithFormat:@"%@ / %@",libelleMarque,libelleModele]];
    appDelegate.marque_id = idMarque;
    appDelegate.modele_id = idModele;
    appDelegate.libelleMarque = libelleMarque;
    appDelegate.libelleModele = libelleModele;
    
}



- (void) receiveValueSelected:(int)valueSelected
                andTypeValeur:(int)type
{
    
    
    if(type == TYPE){
        appDelegate.selectedValueCategorie = valueSelected;
        if(valueSelected != -1){
            Categorie * c = [typesAn objectAtIndex:valueSelected];
            [self.valueCategorie setText:c.libelle];
            [self.valueCategorieMoteur setText:c.libelle];
            [self.valueCategorieDivers setText:c.libelle];
            appDelegate.categorie_id = c.id;
            
        }else{
            appDelegate.categorie_id = [NSString stringWithFormat:@"%01d",-1];
            [self.valueCategorie setText:@"Indifférent"];
            [self.valueCategorieMoteur setText:@"Indifférent"];
            [self.valueCategorieDivers setText:@"Indifférent"];
            
        }
    }
    
    if(type == ETAT){
        appDelegate.selectedValueEtat = valueSelected;
        
        if(valueSelected != -1){
            Etat * e = [appDelegate.donnees.etats objectAtIndex:valueSelected];
            [self.valueEtat setText:e.nomEtat];
            [self.valueEtatMoteur setText:e.nomEtat];
            
            
            appDelegate.etat_id = e.id;
        }else{
            appDelegate.etat_id = [NSString stringWithFormat:@"%01d",-1];
            
            [self.valueEtat setText:@"Indifférent"];
            [self.valueEtatMoteur setText:@"Indifférent"];
            
        }
    }
    
    
    if(type == REGION){
        appDelegate.selectedValueRegion = valueSelected;
        
        if(valueSelected != -1){
            Region * r = [appDelegate.donnees.regions objectAtIndex:valueSelected];
            [self.valueRegion setText:r.nom];
            [self.valueRegionDivers setText:r.nom];
            [self.valueRegionMoteur setText:r.nom];
            
            appDelegate.region_id = r.id;
        }else{
            
            appDelegate.region_id = [NSString stringWithFormat:@"%01d",-1];
            [self.valueRegion setText:@"Indifférent"];
            [self.valueRegionDivers setText:@"Indifférent"];
            [self.valueRegionMoteur setText:@"Indifférent"];
            
        }
    }
    
    
    if(type == MARQUES){
        appDelegate.selectedValueMarques = valueSelected;
        
        if(valueSelected != -1){
            NSArray * marques = [appDelegate.donnees.marques objectForKey:[NSString stringWithFormat:@"%01d",self.typeAnnonce] ] ;
            Marque  * m = [marques objectAtIndex:valueSelected];
            
            [self.valueMarque setText:m.libelle];
            [self.valueMarqueMoteur setText:m.libelle];
            
            appDelegate.marque_id = m.id;
        }else{
            
            appDelegate.marque_id = [NSString stringWithFormat:@"%01d",-1];
            [self.valueMarque setText:@"Indifférent"];
            [self.valueMarqueMoteur setText:@"Indifférent"];
            
        }
    }
}

- (void) receiveDonneesChargement:(id)donnees
                         andType :(int)typeDonnees{
    
    if(typeDonnees == etats){
        appDelegate.donnees.etats = (NSArray *) donnees;
        [self demanderEtat];
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
    }
    
    if(typeDonnees == regions){
        appDelegate.donnees.regions = (NSArray *) donnees;
        [self demanderRegion];
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
    }
}

-(void) effacerValues:(NSArray *) values
            withValue:(NSString *) value
{
    
    for (id valeur in values)
    {
        
        if([valeur isKindOfClass:[UITextField class]]){
            UITextField * tmp = (UITextField *) valeur;
            [tmp setPlaceholder:value];
            [tmp setText:@""];
        }else{
            
            if([valeur isKindOfClass:[UILabel class]]){
                UILabel * tmp = (UILabel *) valeur;
                [tmp setText:value];
                
            }
        }
    }
    
    
    
    appDelegate.categorie_id = nil;
    appDelegate.region_id = nil;
    appDelegate.marque_id = nil;
    appDelegate.modele_id = nil;
    appDelegate.prix_min_range = nil;
    appDelegate.prix_max_range = nil;
    appDelegate.longueur_min_range = nil;
    appDelegate.longueur_max_range = nil;
    appDelegate.puissance_min_range = nil;
    appDelegate.puissance_max_range = nil;
}



- (void) receiveDonneesRBGRange:(NSString *)minValue
                   andNomModele:(NSString *)maxValue{
    
    if(appDelegate.type_range == FILTRE_PRIX){
        
        appDelegate.prix_min_range = minValue;
        appDelegate.prix_max_range = maxValue;
        
        if([maxValue isEqualToString:@"+"]){
            
            [self.valuePrix setText:[NSString stringWithFormat:@"de %@ à %f €+",minValue,appDelegate.maxValue]];
            
            [self.valuePrixMoteur setText:[NSString stringWithFormat:@"de %@ à %f €+",minValue,appDelegate.maxValue]];
            
            
            [self.valueDiversPrix setText:[NSString stringWithFormat:@"de %@ à %f €+",minValue,appDelegate.maxValue]];
            
        }else{
            [self.valuePrix setText:[NSString stringWithFormat:@"de %@ à %@",minValue,maxValue]];
            
            [self.valuePrixMoteur setText:[NSString stringWithFormat:@"de %@ à %@",minValue,maxValue]];
            
            
            [self.valueDiversPrix setText:[NSString stringWithFormat:@"de %@ à %@",minValue,maxValue]];
        }
    }
    
    
    if(appDelegate.type_range == FILTRE_LONGUEUR){
        
        appDelegate.longueur_min_range = minValue;
        appDelegate.longueur_max_range = maxValue;
        
        if([maxValue isEqualToString:@"+"])
            [self.valueLongueur setText:[NSString stringWithFormat:@"de %@ à 16+",minValue]];
        else
            [self.valueLongueur setText:[NSString stringWithFormat:@"de %@ à %@",minValue,maxValue]];
    }
    
    if(appDelegate.type_range == FILTRE_PUISSANCE){
        
        appDelegate.puissance_min_range = minValue;
        appDelegate.puissance_max_range = maxValue;
        
        if([maxValue isEqualToString:@"+"])
            [self.valuePuissance setText:[NSString stringWithFormat:@"de %@ à 300ch+",minValue]];
        else
            [self.valuePuissance setText:[NSString stringWithFormat:@"de %@ à %@",minValue,maxValue]];
    }
}


@end
